#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class SubsidyForEmployeeProvider : CY.GFive.Core.Providers.DALProvider.ISubsidyForEmployeeProvider
	{
		#region Sql
		private static readonly string SqlSelectSubsidyForEmployee = "SELECT [Id], [SubsidyCode],[SubsidyName],[SubsidyDescription],[BeginDate] FROM [SubsidyForEmployee] WHERE  [Id]=@Id";

		private static readonly string SqlInsertSubsidyForEmployee = "INSERT INTO [SubsidyForEmployee]([SubsidyCode],[SubsidyName],[SubsidyDescription],[BeginDate]) values(@SubsidyCode,@SubsidyName,@SubsidyDescription,@BeginDate);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateSubsidyForEmployee = "UPDATE [SubsidyForEmployee] SET [SubsidyCode] = @SubsidyCode,[SubsidyName] = @SubsidyName,[SubsidyDescription] = @SubsidyDescription,[BeginDate] = @BeginDate WHERE  [Id]=@Id";

		private static readonly string SqlDeleteSubsidyForEmployee = "DELETE [SubsidyForEmployee] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllSubsidyForEmployee = "SELECT [Id], [SubsidyCode],[SubsidyName],[SubsidyDescription],[BeginDate] FROM [SubsidyForEmployee]";
		#endregion

		#region ISubsidyForEmployeeProvider Members
		public Core.Business.SubsidyForEmployee Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectSubsidyForEmployee);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.SubsidyForEmployee subsidyForEmployee = new Core.Business.SubsidyForEmployee();

				if (!reader.IsDBNull(0)) subsidyForEmployee.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) subsidyForEmployee.SubsidyCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) subsidyForEmployee.SubsidyName = reader.GetString(2);
				if (!reader.IsDBNull(3)) subsidyForEmployee.SubsidyDescription = reader.GetString(3);
				if (!reader.IsDBNull(4)) subsidyForEmployee.BeginDate = reader.GetDateTime(4);

				return subsidyForEmployee;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.SubsidyForEmployee subsidyForEmployee)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@SubsidyCode", SqlDbType.NVarChar, subsidyForEmployee.SubsidyCode);
			sql.AddParameter("@SubsidyName", SqlDbType.NVarChar, subsidyForEmployee.SubsidyName);
			sql.AddParameter("@SubsidyDescription", SqlDbType.NVarChar, subsidyForEmployee.SubsidyDescription);
			sql.AddParameter("@BeginDate", SqlDbType.DateTime, subsidyForEmployee.BeginDate);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertSubsidyForEmployee);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) subsidyForEmployee.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.SubsidyForEmployee subsidyForEmployee)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@SubsidyCode", SqlDbType.NVarChar, subsidyForEmployee.SubsidyCode);
			sql.AddParameter("@SubsidyName", SqlDbType.NVarChar, subsidyForEmployee.SubsidyName);
			sql.AddParameter("@SubsidyDescription", SqlDbType.NVarChar, subsidyForEmployee.SubsidyDescription);
			sql.AddParameter("@BeginDate", SqlDbType.DateTime, subsidyForEmployee.BeginDate);
			sql.AddParameter("@Id", SqlDbType.Int, subsidyForEmployee.Id);
			sql.ExecuteSql(SqlUpdateSubsidyForEmployee);
		}

		public void Delete(Core.Business.SubsidyForEmployee subsidyForEmployee)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, subsidyForEmployee.Id);
			sql.ExecuteSql(SqlDeleteSubsidyForEmployee);
		}

		public IList<Core.Business.SubsidyForEmployee> GetAllSubsidyForEmployee()
		{
			IList<Core.Business.SubsidyForEmployee> subsidyForEmployeelist = new List<Core.Business.SubsidyForEmployee>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllSubsidyForEmployee);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.SubsidyForEmployee subsidyForEmployee = new Core.Business.SubsidyForEmployee();

					if (!reader.IsDBNull(0)) subsidyForEmployee.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) subsidyForEmployee.SubsidyCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) subsidyForEmployee.SubsidyName = reader.GetString(2);
					if (!reader.IsDBNull(3)) subsidyForEmployee.SubsidyDescription = reader.GetString(3);
					if (!reader.IsDBNull(4)) subsidyForEmployee.BeginDate = reader.GetDateTime(4);

					subsidyForEmployee.MarkOld();
					subsidyForEmployeelist.Add(subsidyForEmployee);
				}
				reader.Close();
			}
			return subsidyForEmployeelist;
		}
		#endregion
	}
}
